package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.BannerDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminBannerServlet extends BaseServlet {
	/**
	 * 显示轮播图
	 * localhost：8080/shangcheng/adminBanner?reqName=getBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BannerDaoImpl bDao=new BannerDaoImpl();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Banner> bList=bDao.getBannerByPage(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(bList));
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 删除轮播图
	 * localhost：8080/shangcheng/adminBanner?reqName=delBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int bId=0;
		if(request.getParameter("bId")!=null && !request.getParameter("bId").equals("")) {
			bId=Integer.parseInt(request.getParameter("bId"));
		}
		
		request.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		BannerDaoImpl bDao=new BannerDaoImpl();
		int n=bDao.delBanner(bId);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 添加轮播图
	 * localhost：8080/shangcheng/adminBanner?reqName=addBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		String bannerimg=request.getParameter("bannerimg");
		BannerDaoImpl bDao=new BannerDaoImpl();
		Banner t=new Banner(0, bannerimg);
		System.out.println(t);
		int n=bDao.addBanner(t);
		
		if(n>0) {
			out.print(Result.toClient(true,"添加成功",""));
		}else {
			out.print(Result.toClient(true,"添加失败",""));
		}
		out.flush();
		out.close();
	}
	/**
	 * 添加图片
	 * localhost:8080/shangcheng/adminBanner?reqName=upload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out= response.getWriter();//out对象
		
		//创建smartUpload对象
		SmartUpload smart=new SmartUpload();
		//设置编码格式
		smart.setCharset("UTF-8");
		//初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//上传
			smart.upload();
			//设置上传文件名
			String fileName=System.currentTimeMillis()+".jpg";
			//将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("images/"+fileName);
			//响应数据
			out.print(Result.toClient(true, "上传成功",fileName));
		}catch(SmartUploadException e){
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
	}
	
	/**
	 * 添加图片
	 * localhost:8080/shangcheng/adminBanner?reqName=getBannerId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBannerId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		BannerDaoImpl bDao=new BannerDaoImpl();
		int bId=0;
		if(request.getParameter("bId")!=null && !request.getParameter("bId").equals("")) {
			bId=Integer.parseInt(request.getParameter("bId"));
		}
		
		Banner n=bDao.getBannerId(bId);
		
		Gson gson=new Gson();
		String res=gson.toJson(n);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * 批量删除
	 * localhost:8080/shangcheng/adminBanner?reqName=deleteBannerByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteBannerByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		BannerDaoImpl bDao=new BannerDaoImpl();
		String ids = request.getParameter("ids");
		int n = bDao.deleteBannerByIds(ids);
		
		if(n>0) {
			out.print(Result.toClient(true,"修改成功",""));
		}else {
			out.print(Result.toClient(false,"修改失败",""));
		}
		out.flush();
		out.close();
	}
	
	/**
	 *  修改轮播图
	 * localhost:8080/shangcheng/adminBanner?reqName=upDateBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upDateBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BannerDaoImpl bDao=new BannerDaoImpl();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		int bannerId=0;
		if(request.getParameter("bannerId")!=null && !request.getParameter("bannerId").equals("")) {
			bannerId=Integer.parseInt(request.getParameter("bannerId"));
		}
		
		String bannerimg=request.getParameter("bannerimg");
		
		Banner d=new Banner(bannerId, bannerimg);
		int n=bDao.updateBanner(d);
		if(n>0) {
			out.print(Result.toClient(true,"修改成功",""));
		}else {
			out.print(Result.toClient(false,"修改失败",""));
		}
		out.flush();
		out.close();
	}
}
